package com.gzk.user.service.impl;

import com.gzk.basic.constant.BaseConstant;
import com.gzk.basic.exception.BusinessException;
import com.gzk.basic.service.impl.BaseServiceImpl;
import com.gzk.basic.util.MD5Utils;
import com.gzk.basic.util.StrUtils;
import com.gzk.user.domain.Logininfo;
import com.gzk.user.domain.User;
import com.gzk.user.dto.UserDto;
import com.gzk.user.mapper.LogininfoMapper;
import com.gzk.user.mapper.UserMapper;
import com.gzk.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public User loadByUserInfo(String userInfo) {
        return userMapper.getByUserInfo(userInfo);
    }

    @Override
    public User loadByPhone(String phone) {
        return userMapper.getByPhone(phone);
    }

    @Override
    public void register(UserDto userDto) {
        //判断是否为空
        if (StringUtils.isEmpty(userDto.getUsername()) ||
                StringUtils.isEmpty(userDto.getVerifyCode()) ||
                StringUtils.isEmpty(userDto.getPassword()) ||
                StringUtils.isEmpty(userDto.getConfirmPassword())||
                StringUtils.isEmpty(userDto.getType())
                ) {
            throw new BusinessException("参数不能为空！！");
        }
        //判断两次密码是否一致
        if(!userDto.getPassword().equals(userDto.getConfirmPassword())){
            throw new BusinessException("两次输入的密码不一致！！！");
        }

        //判断验证码对不对
        String codeVal = (String)redisTemplate.opsForValue().get(BaseConstant.BUSINESS_REGISTER_PREFIX + userDto.getUsername());

        if(StringUtils.isEmpty(codeVal)){
            throw new BusinessException("验证码已过期，请重新获取！");
        }
        String code = codeVal.split(":")[0];

        if(!code.equalsIgnoreCase(userDto.getVerifyCode())){
            throw new BusinessException("验证码不正确！！！");
        }

        //判断数据库里面用户信息是否存在
        if (loadByUserInfo(userDto.getUsername()) != null) {
            throw new BusinessException("用户已经被注册，请直接登录！！");
        }

        //如果用户不存在，进行注册
        User user = userDto2User(userDto);
        Logininfo logininfo = user2Logininfo(user);

        //添加登录信息
        logininfoMapper.save(logininfo);
        user.setLogininfo_id(logininfo.getId());
        //添加用户
        userMapper.save(user);

    }

    @Override
    public User loadByEmail(String email) {
        return userMapper.getByEmail(email);
    }

    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();

        BeanUtils.copyProperties(user, logininfo);
        return logininfo;
    }

    private User userDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getUsername());

        //判断是邮箱注册还是手机注册
        if("phoneRegister".equals(userDto.getType())){
            user.setPhone(userDto.getUsername());
        }else if ("emailRegister".equals(userDto.getType())){
            user.setEmail(userDto.getUsername());
        }
        String salt = StrUtils.getComplexRandomString(32);
        //加密加盐
        String password = MD5Utils.encrypByMd5(userDto.getPassword() + salt);

        user.setSalt(salt);
        user.setPassword(password);
        return user;
    }

}
